// import Api from "./api/Api";
import * as vscode from 'vscode';
import AppMgr from './manager/AppMgr';
import OutputView from './view/OutputView';
import { Config } from './config/Config';
import EmptyLineView from './view/EmptyLineView';
import Setting from './config/Setting';

export default class App {
    // private static mgr: AppMgr;
    private static view: OutputView | EmptyLineView;

    private static initView(): boolean {

        switch (Setting.readModel) {
            case 2:
                this.view = new EmptyLineView();
                break;
            default:
                this.view = new OutputView();
                break;
        }

        return this.view.init();
    }

    private static refreshView() {
        if (!AppMgr._isStart) {
            return;
        }

        if (AppMgr._isHide) {
            this.view.hide();
            return;
        }

        let _data = AppMgr.getCurPageData(this.view.getLineNum());
        this.view.show(_data.content);
    }

    public static start() {
        if (AppMgr._isStart) {
            vscode.window.showErrorMessage("已经打开");
            return;
        }

        if (!this.initView()) {
            return;
        }


        vscode.window.showOpenDialog({
			canSelectMany: false,
			canSelectFolders: false,
			filters: {
				txt: ["txt"]
			}
		})
        .then((val) => {
			let _ph = val && val[0] ? val[0] : undefined;
			if (_ph) {
				AppMgr.initBook(_ph);
                AppMgr._isStart = true;
                this.refreshView();
			}
			else {
				vscode.window.showErrorMessage("没有书！");
			}
		});
    }

    public static exit() {
        if (!AppMgr._isStart) {
            return;
        }
        AppMgr._isStart = false;
        this.view.hide();
        AppMgr.destory();
    }

    public static toNext() {
        if (!AppMgr._isStart || AppMgr._isHide) {
            return;
        }
        AppMgr.setToNextPage(this.view.getLineNum());
        AppMgr.updateAutoMark();
        this.refreshView();
    }

    public static toLast() {
        if (!AppMgr._isStart || AppMgr._isHide) {
            return;
        }
        AppMgr.setToLastPage(this.view.getLineNum());
        AppMgr.updateAutoMark();
        this.refreshView();
    }

    public static switchBoss() {
        if (!AppMgr._isStart) {
            return;
        }
        AppMgr._isHide = !AppMgr._isHide;
        if (AppMgr._isHide) {
            this.view.hide();
        }
        else {
            this.refreshView();
        }
    }

    public static jumpTo() {
        if (!AppMgr._isStart || AppMgr._isHide) {
            return;
        }

        vscode.window.showInputBox({placeHolder: "请输入跳转位置"})
        .then((val) => {
            if (typeof val !== "string") {return;}
            if (/^:[0-9]+$/gi.test(val)) {
                // 跳转至指定行
                let _index = Number(val.substring(1));
                AppMgr.jumpToIndex(_index);

            }
            else {
                // 跳转至匹配位置
                AppMgr.jumpToMatch(val);
            }
            AppMgr.updateAutoMark();
            this.refreshView();
        });
    }

    public static async showSetting() {
        if (!AppMgr._isStart) {
            return vscode.window.showErrorMessage("fishReader尚未启动!");
        }
        const _sel1 = await vscode.window.showQuickPick(Config.SettingConst, {placeHolder: "选择设置的项目"});
        if (!_sel1) {return;}
        switch (_sel1.id) {
            case 1:
                this.doSetLineChar();
                break;
            case 2:
                this.doSetPageLine();
                break;
            default:
                break;
        }
    }

    private static doSetLineChar() {
        vscode.window.showInputBox({placeHolder: "请输入单行最大字符数"})
        .then((val) => {
            let _num = Number(val);
            if (_num && _num > 0) {
                AppMgr.setLineChar(_num);
                this.refreshView();
            }
            else {
                vscode.window.showErrorMessage("请输入正整数");
            }
        });
    }

    private static doSetPageLine() {
        vscode.window.showInputBox({placeHolder: "请输入单页最大行数"})
        .then((val) => {
            let _num = Number(val);
            if (_num && _num > 0) {
                AppMgr.setPageLine(_num);
                this.refreshView();
            }
            else {
                vscode.window.showErrorMessage("请输入正整数");
            }
        });
    }

}